﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

using XGameFrame.Graphic;
using XGameFrame.Controls;

namespace XGameFrame.Collision
{
    /// <summary>
    /// Static Class to detect Collision
    /// </summary>
    public static class CollisionHandler
    {
        /// <summary>
        /// Detects collisions between two CoreGraphicElements jahaa
        /// </summary>
        /// <param name="pSpriteOne">First sprite</param>
        /// <param name="pSpriteTwo">Second Sprite</param>
        /// <returns>True if collision is detected, else false</returns>
        public static Boolean CheckOpaqueCollison(CoreGraphicElement pSpriteOne, CoreGraphicElement pSpriteTwo)
        {
            //Checks if two sprites overlapping each other (only the currentframe)
            Rectangle collisionCheck = Rectangle.Intersect(pSpriteOne.Bounds, pSpriteTwo.Bounds);
            if(collisionCheck.IsEmpty)
                return false; //no collision possible if there is no overlapping area

            //initialisize two colorarrays to save each pixeldata of both currentframetextures
            Color[] spriteImageDataOne = new Color[pSpriteOne.SourceRectangle.Height * pSpriteOne.SourceRectangle.Width]; //totalpixels = texturewidth * textureheight
            Color[] spriteImageDataTwo = new Color[pSpriteTwo.SourceRectangle.Height * pSpriteTwo.SourceRectangle.Width];
            //get each pixeldata for the actually drawed area
            pSpriteOne.Texture.GetData(0, pSpriteOne.SourceRectangle, spriteImageDataOne, 0, spriteImageDataOne.Length);
            pSpriteTwo.Texture.GetData(0, pSpriteTwo.SourceRectangle, spriteImageDataTwo, 0, spriteImageDataTwo.Length);

            //iterate through overlapping area -> pixel for pixel
            for(int y = collisionCheck.Top ; y < collisionCheck.Bottom ; y++)
            {
                for(int x = collisionCheck.Left ; x < collisionCheck.Right ; x++)
                {
                    //detects if pixel are not transparent
                    if(spriteImageDataOne[(y - pSpriteOne.Bounds.Top) * pSpriteOne.Bounds.Width + (x - pSpriteOne.Bounds.Left)].A != 0 && 
                       spriteImageDataTwo[(y - pSpriteTwo.Bounds.Top) * pSpriteTwo.Bounds.Width + (x - pSpriteTwo.Bounds.Left)].A != 0)
                        return true; //collision is true
                }
            }
            return false;
        }


        public static Boolean CheckOpaqueCollison(Control pText, CoreGraphicElement pSpriteTwo)
        {
            //Checks if two sprites overlapping each other (only the currentframe)
            Rectangle collisionCheck = Rectangle.Intersect(pText.Bounds, pSpriteTwo.Bounds);
            if (collisionCheck.IsEmpty)
                return false; //no collision possible if there is no overlapping area
            else
                return true;
        }

    }
}
